package com.sky.demo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.realogic.btp.dhtmlx.DhtmlxErrorXml;
import org.realogic.btp.dhtmlx.DhtmlxGridXml;
import org.realogic.cfm.KAppContext;
import org.realogic.cfm.domain.KPageResult;
import org.realogic.cfm.persistence.query.QueryCondition;
import org.realogic.cfm.persistence.query.QueryConditionFactory;
import org.realogic.cfm.util.LongUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sky.demo.entity.EntityUom;
import com.sky.demo.service.EntityUomService;

@Controller
@RequestMapping("/entityUom")
public class EntityUomController {

	private EntityUomService entityUomService;

	public EntityUomService getEntityUomService() {
		return entityUomService;
	}

	@Autowired
	public void setEntityUomService(EntityUomService entityUomService) {
		this.entityUomService = entityUomService;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute(new EntityUom());
		return "plugin/demo/EntityUomEdit";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@Validated EntityUom aValue) {
		aValue.setId(LongUUID.uuidOfAbsLongs());
		entityUomService.create(aValue);
		return "redirect:/entityUom/list.do";
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(@RequestParam(value = "key", required = true) Long id, Model model) {
		model.addAttribute("entityUom", entityUomService.read(id));
		return "plugin/demo/EntityUomEdit";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(EntityUom aValue, BindingResult result) {
		entityUomService.update(aValue);
		return "redirect:/entityUom/list.do";
	}

	@RequestMapping(value = "/remove")
	@ResponseBody
	public String remove(@RequestParam(value = "key", required = true) Long id) {
		entityUomService.remove(id);
		return "success";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "plugin/demo/EntityUomList";
	}

	@RequestMapping(value = "/datagrid")
	public void datagrid(HttpServletRequest request, HttpServletResponse response) {
		KPageResult result = null;
		DhtmlxGridXml xmler = null;
		QueryCondition condition = QueryConditionFactory.createQueryCondition(request);
		String grid = request.getParameter("grid");
		try {
			result = entityUomService.query(condition);
			if (grid == null || "".equals(grid)) {
				xmler = new DhtmlxGridXml("EntityUomGrid", result,KAppContext.currentUser());
			} else {
				xmler = new DhtmlxGridXml(grid, result,KAppContext.currentUser());
			}
			response.setContentType("text/xml");
			response.setCharacterEncoding("utf-8");
			xmler.toXml(response.getWriter());
		} catch (Exception e) {
			try {
				response.setContentType("text/xml");
				response.setCharacterEncoding("utf-8");
				DhtmlxErrorXml.toExceptionXml(response.getWriter(), e);
			} catch (Exception ee) {

			}
		}
	}

}